﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using ExtAspNet;
using Sale_Operation;
using Sale_Model;
using Sale_Common;

namespace SaleSystem
{
    public partial class main : PageBase
    {
        #region Page Event
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {


                bnExit.OnClientClick = "window.location='index.aspx';return false;";
                bnStyle.OnClientClick = win_Content.GetShowReference("./Other/frm_StyleList.aspx", "主题切换");
                GetAndBind();
                GetWrokPlan();
                GetHomeUrl();
            }
        }

        public void timer_getInfo_Tick(object sender, EventArgs e)
        {
          
        }
        /// <summary>
        /// Change theme.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlTheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            //HttpCookie themeCookie = new HttpCookie("Theme", ddlTheme.SelectedValue);
            //themeCookie.Expires = DateTime.Now.AddYears(1);
            //Response.Cookies.Add(themeCookie);
            //PageContext.Refresh();
        }
        /// <summary>
        /// 一但有子窗口打开，该按钮报错
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void bnExitClick(object sender, EventArgs e)
        {
            Session.Abandon();
            Response.Redirect("index.aspx");
        }

        #endregion
        private void GetHomeUrl()
        {
            SysConfigOp Manager = new SysConfigOp();
            SysConfig SM = new SysConfig();
            SM.SConf_Id = 6;
            Manager.GetConfig(SM);
            this.tab_Home.IFrameUrl= SM.SConf_Value;
        }
        private void GetWrokPlan()
        {
            //工作计划
            WorkPlanOp workManager = new WorkPlanOp();
            DataTable dt = workManager.GetList("WPlan_WarningTime<='" + System.DateTime.Now.ToString("yyyy-MM-dd") + "' AND WPlan_Enable=1 AND WPlan_State=0 AND A.State=1 ");
            if (dt.Rows.Count > 0)
            {
                WorkForm.Title = "您最近有<font color='red'>" + dt.Rows.Count.ToString() + "</font>件事情需要处理！";
                bnWorkFlow.Text = "(<font color='red'>" + dt.Rows.Count + "</font>)";
                bnWorkFlow.ToolTip = "您最近有<font color='red'>" + dt.Rows.Count.ToString() + "</font>件事情需要处理！";
                PageContext.RegisterStartupScript("GetPosition();");

            }
        }

        private void GetAndBind()
        {
            //获取系统名称
            SysConfigOp sysConfigOp = new SysConfigOp();
            SysConfig config = new SysConfig();
            config.SConf_Id = (int)EConfig.系统名称;
            sysConfigOp.GetConfig(config);
            lbSystemInfo.Text = config.SConf_Value;
            config.SConf_Id = (int)EConfig.版本号;
            sysConfigOp.GetConfig(config);
            this.Title = lbSystemInfo.Text + config.SConf_Value;
            lbSystemInfo.Text = lbSystemInfo.Text + "&nbsp;" + config.SConf_Value;
       
            //获取登陆信息
            tb_UserName.Text = "欢迎 &nbsp;<font color='red' bold='true'>" + LinkService.GetCurrentUser().SUser_Name + "</font>&nbsp;登陆系统！";
            SysMenuOp menuOp = new SysMenuOp();
            List<SysMenu> LSMenu = menuOp.GetList();
            BindTree(LSMenu, 0, tv_Menu.Nodes);
            tv_Menu.Expanded = true;
            tv_Menu.ExpandAllNodes();
        }
        private void BindTree(List<SysMenu> LS, int id,ExtAspNet.TreeNodeCollection tns)
        {
            List<SysMenu> LS1 = LS.FindAll(t => t.SMenu_PId == id);
            for (int i = 0; i < LS1.Count; i++)
            {
                ExtAspNet.TreeNode tn = new ExtAspNet.TreeNode();
                tn.NodeID = LS1[i].SMenu_Id.ToString();
                tn.Text = LS1[i].SMenu_Title;
                if (LS1[i].SMenu_PId == 0)
                    tn.Icon = Icon.Folder;
                else
                {
                    tn.Icon = Icon.NoteEdit;
                    tn.Leaf = true;
                }
                tn.NavigateUrl = LS1[i].SMenu_Page;
                tn.Target = "main";
                tns.Add(tn);

                BindTree(LS, LS1[i].SMenu_Id, tn.Nodes);                
            }
        }

    }
}
